import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path:'/',
    //重定向到login
  },
  {
    path: '/home',
    name: 'home',
    component: Home,
  },
  {
    path: '/about',
    name: 'about',
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "about" */ '../views/login.vue'),
  },
  {  // 首页
    path: '/welcome',
    name: 'welcome',
    component: () => import(/* webpackChunkName: "about" */ '../views/welcome.vue'),
    children: [
      {  //用户列表
        path: '/users',
        name: 'users',
        component: () => import(/* webpackChunkName: "about" */ '../views/users.vue')
      },
      {  //首页
        path: '/',
        name: 'home',
        component: () => import(/* webpackChunkName: "about" */ '../views/home.vue'),
      },
      {  //角色列表
        path: '/roles',
        name: 'roles',
        component: () => import(/* webpackChunkName: "about" */ '../views/roles.vue'),
      },
      {  //权限列表
        path: '/rights',
        name: 'rights',
        component: () => import(/* webpackChunkName: "about" */ '../views/rights.vue'),
      },
      {  //商品列表
          path: '/goods',
          name: 'goods',
          component: () => import(/* webpackChunkName: "about" */ '../views/goods.vue'),
      },
      {  //添加商品
        path: '/goods/add',
        name: 'goods/add',
        component: () => import(/* webpackChunkName: "about" */ '../views/add.vue'),
      },
      {  //分类参数
        path: '/params',
        name: 'params',
        component: () => import(/* webpackChunkName: "about" */ '../views/params.vue'),
      },
      { //商品分类
        path:'/categories',
        name:'categories',
        component: () => import(/* webpackChunkName: "about" */ '../views/categories.vue'),
      },
      { //订单列表
        path:'/orders',
        name:'orders',
        component: () => import(/* webpackChunkName: "about" */ '../views/orders.vue'),
      },
      { //数据报表
        path:'/report',
        name:'report',
        component: () => import(/* webpackChunkName: "about" */ '../views/report.vue'),
        meta:{
          title:'数据报表'
        }
      }
    ]
  },
  {
    path:'/myinput',
    name:'myinput',
    component: () => import(/* webpackChunkName: "about" */ '../components/myinput.vue'),
    meta:{
      title:'input组件'
    }
  }
]

const router = new VueRouter({
  routes
})


//全局前置路由守卫
router.beforeEach((to,from,next)=>{
  // console.log(to,from);
  if(to.path == '/login'){
    next()
  }else if(sessionStorage.getItem('token')){
    next()
  }else{
    next('/login')
  }
})

export default router
